// 这里直接可以从passport官网中直接粘贴
var JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require('mongoose')
const User = mongoose.model('users')
const keys = require('../config/keys.js')


var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrkey;

module.exports = (passport)=>{
  passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
    // console.log(jwt_payload)
    User.findById(jwt_payload.id).then(user=>{
      if(user){
        return done(null,user)
      }else{
        return done(null,false)
      }
    }).catch(err=>console.log(err))
}))
}